package com.wontube.smr.web.controller.admin;

import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.extension.remote.cache.ICacheHandler;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.IDeviceHandler;
import com.wontube.smr.client.model.po.DeviceExpert;
import com.wontube.smr.web.client.admin.IAdminDeviceHandler;


/**
 * 
 * @author LuoBiao
 *
 */
@Controller
@RequestMapping(value = "/admin/device", method = RequestMethod.GET)
public class AdminDeviceController {	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public ModelAndView adminDeviceIndex(){
		ModelAndView mv = new ModelAndView("admin/device/device_index");
		Remoting<IDeviceHandler> remoting = new Remoting<IDeviceHandler>();
		IDeviceHandler handler = remoting.createService(IDeviceHandler.class);
		
		List<DeviceExpert> deviceList = handler.getAllSimulateAgent();
		mv.addObject("deviceList",deviceList);		
		return mv;
	}
	
	@RequestMapping(value="/add",method = RequestMethod.POST)
	@ResponseBody
	public String addDevice(DeviceExpert de){				
		Remoting<IAdminDeviceHandler> remoting = new Remoting<IAdminDeviceHandler>();
		IAdminDeviceHandler handler = remoting.createService(IAdminDeviceHandler.class);
		de.setId(UUID.randomUUID().toString());
		boolean result = handler.saveOrUpdateDevice(de);
		if(result){
			return "Done";
		}else{
			return "Something Wrong";
		}		
	}
	
	@RequestMapping("/clean")
	@ResponseBody
	public String deleteCache_apply(){
		Remoting<ICacheHandler> remoting = new Remoting<ICacheHandler>();
		remoting.setDataFormat(Remoting.dataFormatHessian);
		ICacheHandler handler = remoting.createService(ICacheHandler.class);
		String clazzName = IDeviceHandler.class.getName();
		String methodName = "getSimulateAgent";
		handler.deleteAll(clazzName, methodName, 2);
		methodName = "isDownSupport";
		handler.deleteAll(clazzName, methodName, 2);
		methodName = "getAllSimulateAgent";
		handler.delete(clazzName, methodName, new Object[0]);		
		
		if(remoting.isDone()){
			return "Done";
		}else{
			return "Something Wrong";
		}
	}
	
	@RequestMapping(value="/get/{pk}",method = RequestMethod.GET)
	public ModelAndView gotoUpdateDevice(@PathVariable("pk") String pk){
		Remoting<IAdminDeviceHandler> remoting = new Remoting<IAdminDeviceHandler>();
		IAdminDeviceHandler handler = remoting.createService(IAdminDeviceHandler.class);
		DeviceExpert de = handler.searchDeviceByPK(pk);
		ModelAndView mv = new ModelAndView();
		mv.addObject("device", de);
		mv.setViewName("admin/device/update_device");
		return mv;
	}
	
	
	@RequestMapping(value="/update",method = RequestMethod.POST)
	@ResponseBody
	public String updateDevice(DeviceExpert de){
		Remoting<IAdminDeviceHandler> remoting = new Remoting<IAdminDeviceHandler>();
		IAdminDeviceHandler handler = remoting.createService(IAdminDeviceHandler.class);		
		boolean done = handler.saveOrUpdateDevice(de);		
		if(done){
			return "Done";
		}else{
			return "Something Wrong";
		}	
	}	
	
	@RequestMapping(value="/delete/{id}",method = RequestMethod.GET)
	@ResponseBody
	public String deleteDevice(@PathVariable("id") String id){
		Remoting<IAdminDeviceHandler> remoting = new Remoting<IAdminDeviceHandler>();
		IAdminDeviceHandler handler = remoting.createService(IAdminDeviceHandler.class);
		boolean result = handler.deleteDevice(id);
		if(result){
			return "Done";
		}else{
			return "Something Wrong";
		}	
	}
}
